package com.kaka.bus.engine.home.main;

import android.app.Activity;

import com.kaka.bus.bean.DrainProductBean;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.Rolling;
import com.kaka.bus.bean.Status;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.engine.RouterUtil;
import com.kaka.bus.engine.common.impl.LoanService;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by zhangqi on 2016/10/14.
 * e-mail : ${email}
 * desc :
 */

public class MainPresenter extends RxPresenter<MainContract.View> implements MainContract.Presenter {

    private LoanService mLoanService;

    @Inject
    public MainPresenter(DataManager dataManager, LoanService loanService) {
        super(dataManager);
        mLoanService = loanService;
        setService(mLoanService);
    }

    @Override
    public void commitContact(String userId, String json) {
        addSubscribe(mDataManager.getApi()
                .commitContact(userId, json)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> bean) {
                        mView.commitContactSuccess(bean);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }

                }));
    }

    @Override
    public void getRolling(String userId, String token, String platform, String version) {
        addSubscribe(mDataManager
                .getApi()
                .getRolling(userId, token, platform, version)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<Rolling>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<Rolling> rolling) {
                        mView.getRollingSuccess(rolling);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                    }
                }));
    }


    @Override
    public void checkBpm(String userId, String token) {
        mView.showDialog("读取用户信息..");
        mLoanService.checkBpm(userId, token, new Result<HttpWrapper<Status>>() {
            @Override
            protected void onSuccess(HttpWrapper<Status> s) {
                mView.checkBpmSuccess(s);
            }

            @Override
            protected void onFinish() {
                super.onFinish();
                mView.dissDialog();
            }
        });
    }


    @Override
    public void uploadContact(String userId, String json) {
        mLoanService.uploadContact(userId, json, new Result<HttpWrapper<String>>() {
            @Override
            protected void onSuccess(HttpWrapper<String> s) {
                mView.uploadContactSuccess(s);
            }

            @Override
            protected void onFaild() {
                super.onFaild();
                mView.uploadContactFaild();
            }
        });
    }

    @Override
    public void getHotPro(String userId, int page, int pageSize) {
        request(userId, page, pageSize, false);
    }

    @Override
    public void loadMore(String userId, int page, int pageSize) {
        request(userId, page, pageSize, true);
    }

    @Override
    public void chargeApply(String event, String productId, String bannerId, String device, String perId, String chargeProductId) {
        addSubscribe(mDataManager.getApi().chargeApply("view", productId, bannerId, Constant.PLATFORM, perId, "1")
                .compose(T.D())
                .subscribeWith(new Result<String>() {
                    @Override
                    protected void onSuccess(String stringHttpWrapper) {

                    }
                })
        );
    }


    public void request(String userId, int page, int pageSize, boolean isLoadMore) {

        addSubscribe(mDataManager.getApi().getDrain(page, pageSize).compose(T.D())
                .subscribeWith(new Result<HttpWrapper<List<DrainProductBean>>>() {

                    @Override
                    protected void onSuccess(HttpWrapper<List<DrainProductBean>> drainProductBeanHttpWrapper) {
                        if (isLoadMore) {
                            mView.onResultLoadMore(drainProductBeanHttpWrapper);
                        } else {
                            mView.onResultStart(drainProductBeanHttpWrapper);
                        }

                    }

                    @Override
                    protected void onFaild() {
                        super.onFaild();
                        if(isLoadMore){
                            mView.onError();
                        }else{
                            mView.onStartError();
                        }

                    }
                })
        );
    }

    @Override
    public void router(Activity activity) {
        mView.showDialog("查询节点中...");
        RouterUtil.router(activity, mDataManager.getApi(), mCompositeDisposable, () -> {
            if (mView != null) {
                mView.dissDialog();
            }
        });
    }


}